package com.example.springboot_demo.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.example.springboot_demo.pojo.User;
import com.example.springboot_demo.dao.UserDao;
import com.example.springboot_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    @Qualifier("UserDao")
    private UserDao userDao;

    //检查是否存在相同用户名
    @Override
    public String checkUM(User user) {

        user.setUserName(user.getUserName().replace(" ", ""));
        String username = user.getUserName();

        if (userDao.inquire(user) == null && username.length() >= 5 && username.length() <= 20) {
            String usable = JSON.toJSONString("usable");
            return usable;
        } else {
            if (username.equals("")) {
                String disabled = JSON.toJSONString("");
                return disabled;
            } else if (username.length() < 5 || username.length() > 20) {
                String disabled = JSON.toJSONString("用户名不合法");
                return disabled;
            } else {
                String disabled = JSON.toJSONString("用户名已存在");
                return disabled;
            }
        }
    }


    //注册用户
    @Override
    public String register(User user) {

        user.setUserName(user.getUserName().replace(" ", ""));
        user.setUserPassWord(user.getUserPassWord().replace(" ", ""));
        String username = user.getUserName();
        String password = user.getUserPassWord();
        String registerFail = "";

        if (userDao.inquire(user) == null && username.length() >= 5 && username.length() <= 20) {
            if (password.length() >= 8 && password.length() <= 16) {
                String registerSuccess = JSON.toJSONString("注册成功");
                userDao.registerUser(user);

                return registerSuccess;
            } else if (password.length() == 0) {
                registerFail = JSON.toJSONString("密码为空或者全为空格");
                return registerFail;
            } else {
                registerFail = JSON.toJSONString("密码不合法");
                return registerFail;
            }
        } else {
            if (username.equals("")) {
                registerFail = JSON.toJSONString("用户名不能为空");

            } else if (username.length() < 5 || username.length() > 20) {
                registerFail = JSON.toJSONString("用户名不合法");
            } else {
                registerFail = JSON.toJSONString("用户名已存在");
            }
            return registerFail;
        }
    }

    //检查登录, flag为判断是否还需要继续保存密码
    @Override
    public String checkLogin(User user, Boolean flag, HttpServletRequest req, HttpServletResponse resp) {
        //查找是否存在该用户
        User user1 = userDao.checkLogin(user);

        //对admin是否存在来处理业务逻辑
        if (user1 != null) {
            Cookie cookie_username = new Cookie("username", user.getUserName());
            cookie_username.setMaxAge(60 * 60 * 24 * 7);
            resp.addCookie(cookie_username);
            user1.setUserDate(userDao.getUserDate(user));
            if (userDao.isAdmin(user) == 1) {
                if (flag) {
                    Cookie cookie_password = new Cookie("password", user.getUserPassWord());
                    cookie_password.setMaxAge(60 * 60 * 24 * 7);
                    resp.addCookie(cookie_password);

                } else {
                    Cookie[] cookies = req.getCookies();
                    if (cookies != null) {
                        for (Cookie c : cookies) {
                            if (c.getName().equals("password")) {
                                c.setMaxAge(0);
                                resp.addCookie(c);
                            }
                        }
                    }
                }
                return "isAdmin;" + user1.getUserDate();
            } else {
                if (flag) {
                    Cookie cookie_password = new Cookie("password", user.getUserPassWord());
                    cookie_password.setMaxAge(60 * 60 * 24 * 7);
                    resp.addCookie(cookie_password);

                } else {
                    Cookie[] cookies = req.getCookies();
                    if (cookies != null) {
                        for (Cookie c : cookies) {
                            if (c.getName().equals("password")) {
                                c.setMaxAge(0);
                                resp.addCookie(c);
                            }
                        }
                    }
                }
                return "success;" + user1.getUserDate();
            }

        } else {
            String fail = "账户或者密码错误，请检查!";
            return fail;
        }

    }
    public String updatePassword(String userName,String oldPwd,String newPwd1,String newPwd2,HttpServletRequest req,HttpServletResponse resp){
        User user=userDao.getUserByName(userName);
        if(!user.getUserPassWord().equals(oldPwd)){
            String reslut1="原密码错误";
            return reslut1;
        }
        if(newPwd1.length()<6){String resul2="密码至少要为6位";return resul2;}
        if(!newPwd1.equals(newPwd2)){String result3="两次新输入的密码不一致"; return result3;}
        userDao.updatePasswordByName(userName,newPwd1);
        String result="修改成功";
        return result;
    }

}
